package com.suyafei.controller;


import com.suyafei.enums.CouponCategoryEnum;
import com.suyafei.request.NewUserCouponRequest;
import com.suyafei.service.CouponService;
import com.suyafei.util.JsonData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import springfox.documentation.spring.web.json.Json;

import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author suyafei
 * @since 2021-03-03
 */
@Api(tags = "优惠券模块")
@RestController
@RequestMapping("/api/coupon/v1")
public class CouponController {

    @Autowired
    private CouponService couponService;

    @ApiOperation("分页查询优惠券")
    @GetMapping("page_coupon")
    public JsonData pageCouponList(
            @ApiParam(value = "当前页")
            @RequestParam(value = "page", defaultValue = "1") int page,
            @ApiParam(value = "每页显示多少条")
            @RequestParam(value = "size", defaultValue = "10") int size
    ) {
        Map<String, Object> map = couponService.pageCouponActivity(page, size);
        return JsonData.buildSuccess(map);
    }


    @ApiOperation("添加优惠券")
    @GetMapping("/add/promotion/{coupon_id}")
    public JsonData addPromotionCoupon(
            @ApiParam(value = "优惠券id")
            @PathVariable("coupon_id") long couponId) {
        JsonData jsonData = couponService.addCoupon(couponId, CouponCategoryEnum.PROMOTION);
        return jsonData;
    }

    @ApiOperation("RPC-拉新服务")
    @PostMapping("new_user_coupon")
    public JsonData addNewUserCoupon(@RequestBody NewUserCouponRequest newUserCouponRequest) {
        JsonData jsonData = couponService.addNewUserCoupon(newUserCouponRequest);

        return jsonData;
    }
}

